package com.gljx.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public  class TimeFormat {	
	
	public static String timeFormat(Date  date){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createtime = format.format(date);
        return createtime;
    }

	/**
	 * 获取一个月时间
	 * @param date 传进来的天数
	 * @return
	 */
	public static String getMonth(){
		Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
        ca.setTime(new Date()); // 设置时间为当前时间
        ca.add(Calendar.DATE, -2);// 月份减1
        Date date = ca.getTime(); // 结果
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sdf.format(date);
        return beginTime;
    }

	/**
	 * 获取前三天
	 * @param date 当前时间
	 * @return
	 */
	public static String getThreeDay(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -3);
        Date date = calendar.getTime();
        String threeDay = sdf.format(date);
		return threeDay;
	}

    /**
     * @Author lcx
     * @Date 2017/12/1 0001 上午 11:22
     * 获取今天时间
     */
    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String threeDay = sdf.format(date);
        return threeDay;
    }

    public static String getNowTimeAndOne() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_WEEK, +1);
        Date date = calendar.getTime();
        String threeDay = sdf.format(date);
        return threeDay;
    }

    public static String getNowTimeAsMiao() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String threeDay = sdf.format(date);
        return threeDay;
    }

    /**
     * @Author lcx
     * @Date 2017/12/1 0001 上午 11:22
     * 获取下个月时间
     */
    public static String getNowTimeAddOne() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, +1);
        Date date = calendar.getTime();
        String threeDay = sdf.format(date);
        return threeDay;
    }

    /**
     * @Author lcx
     * @Date 2017/12/1 0001 上午 11:22
     * 获取下个月时间
     */
    public static String getLastTimeAddMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date date = calendar.getTime();
        String threeDay = sdf.format(date);
        return threeDay;
    }


   /**
    * 获取本月第一天与最后一天
    * @return
    */
    public static Map<String, String> thisMonth() {
    	Map<String, String> map = new HashMap<String, String>();
    	//时间格式化
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	/*************获取本月最后一天************************/
        //获取Calendar
        Calendar calendar = Calendar.getInstance();
        // 设置日期为本月最大日期
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        //获取本月最后一天
        String endTime = format.format(calendar.getTime());
        //放入集合中
        map.put("endTime", endTime);
        /*************获取本月第一天************************/
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String beginTime = format.format(c.getTime());
        //放入集合中
        map.put("beginTime", beginTime);
        return map;
     }

    /**
     * 计算两个月份差
     * @param minDate
     * @param maxDate
     * @return
     * @throws ParseException
     */
    public static List<String> getMonthBetween(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        try {
			min.setTime(sdf.parse(minDate));
			min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
		    max.setTime(sdf.parse(maxDate));
		    max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        Calendar curr = min;
        while (curr.before(max)) {
         result.add(sdf.format(curr.getTime()));
         curr.add(Calendar.MONTH, 1);
        }
        return result;
      }

    /**
     * @Author lcx
     * @Date 2017/12/28 0028 下午 5:26
     * 获取下一个月
     */
    public static String sumOneMonth(String date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
    	Calendar calendar = Calendar.getInstance();
    	try {
			calendar.setTime(sdf.parse(date));
			calendar.add(Calendar.MONTH, 1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return sdf.format(calendar.getTime());
    }

    //获取指定月份的天数
    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 遍历一个月
     * @return
     */
    public static List<String> getDayListOfMonth(Date month) {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    	List<String> time = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(month);//month 为指定月份任意日期
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH)+1;
        int dayNumOfMonth = TimeFormat.getDaysByYearMonth(year, m);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 从一号开始
        for (int i = 0; i < dayNumOfMonth; i++, cal.add(Calendar.DATE, 1)) {
            Date d = cal.getTime();
            String df = simpleDateFormat.format(d);
            time.add(df);
    	   }  
    	    return time;
    }
   
    /**
	 * 获取给定月的最后第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String getLastdayOfMonth(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);// someDate 为你要获取的那个月的时间
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.add(Calendar.MONTH, 1);
		ca.add(Calendar.DAY_OF_MONTH, -1);
		// 最后一天
		Date lastDate = ca.getTime();
		return simpleDateFormat.format(lastDate);
	}


	/**
	 * 获取给定月的第一天
	 * 
	 * @param date
	 * @return
	 */
	public static String getFirstdayOfMonth(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);// someDate 为你要获取的那个月的时间
		ca.set(Calendar.DAY_OF_MONTH, 1);
		// 第一天
		Date firstDate = ca.getTime();
		return simpleDateFormat.format(firstDate);
	}
    
	/**
	 * 获取两个天数的时间差
	 * @param stime
	 * @param etime
	 * @return
	 */
	public static  List<String> getBetweenDays(String stime,String etime){  
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");  
        Date sdate = null;  
        Date eDate = null;  
        try {  
            sdate = df.parse(stime);  
            eDate = df.parse(etime);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        Calendar c = Calendar.getInstance();  
        List<String> list = new ArrayList<String>();  
        while (sdate.getTime() <= eDate.getTime()) {  
            list.add(df.format(sdate));  
            c.setTime(sdate);  
            c.add(Calendar.DATE, 1); // 日期加1天  
            sdate = c.getTime();  
        }  
        return list;  
  }  

	/**
	 * 根据时间加一天
	 * @param time
	 * @return
	 */
	public static String getTomorrow(String time){
		 SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		  Calendar c = Calendar.getInstance();  
	      try {
			 c.setTime(f.parse(time));
			 c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天  
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
	    Date tomorrow = c.getTime();  
		return f.format(tomorrow);	
	}

    /**
     * @Author lcx
     * @Date 2017/12/1 0001 上午 11:22
     * 获取今天时间
     */
    public static String getNowTime2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String threeDay = sdf.format(date);
        return threeDay;
    }
/*	public static void main(String[] args) {
		List<String> time = getBetweenDays("2017-01-24","2017-02-28");
		for (String string : time) {
			System.out.println(string);
		}
	}*/
	
}
